﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace studentskicentarETFa
{
    partial class DAL
    {
        public class TerminDAO: IDaoCrud<Termin>
        {
            protected MySqlCommand komanda;

            public long create(Termin entitet)
            {
                try
                {
                    MySqlCommand komanda = new MySqlCommand();
                    komanda.CommandText = "insert into Termini values (@ID_Termin,@BrojLjudi,@ID_Instruktor,@ID_Predmeta) ";
                    komanda.Parameters.AddWithValue("@ID_Termin", entitet.ID_Termin);
                    komanda.Parameters.AddWithValue("@BrojLjudi", entitet.BrojLjudi);
                 //   komanda.Parameters.AddWithValue("@DatumOdrzavanja", entitet.DatumOdrzavanja);
                    komanda.Parameters.AddWithValue("@ID_Instruktor", entitet.ID_Instruktor);
                    komanda.Parameters.AddWithValue("@ID_Predmeta", entitet.ID_Predmeta);
                    komanda.Connection = con;
                    komanda.Prepare();
                    komanda.ExecuteNonQuery();
                    con.Close();
                    return 1;

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Termin read(Termin entitet)
            {
                try
                {
                    komanda = new MySqlCommand("select * from Termini where ID_Termin = '" + entitet.ID_Termin.ToString() + "';", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Termin termin = null;
                    while (citac.Read())
                    {
                        termin = new Termin(citac.GetString("ID_Termin"),citac.GetInt32("BrojLjudi"), citac.GetString("ID_instruktor"),citac.GetString("ID_Predmeta"));
                        return termin;
                    }
                    return termin;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Termin update(Termin entitet)
            {
                try
                {
                    komanda.CommandText = "update Termin set ID_Termin=@ID_Termin, BrojLjudi=@BrojLjudi, ID_Instruktor=@ID_Instruktor";
                    komanda.Parameters.AddWithValue("@ID_Termin", entitet.ID_Termin);
                    komanda.Parameters.AddWithValue("@BrojLjudi", entitet.BrojLjudi);
                 //   komanda.Parameters.AddWithValue("@DatumOdrzavanja", entitet.DatumOdrzavanja);
                    komanda.Parameters.AddWithValue("@ID_Instruktor", entitet.ID_Instruktor);

                    komanda.Connection = con;
                    komanda.Prepare();
                    komanda.ExecuteNonQuery();
                    con.Close();
                    return read(entitet);

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public void delete(Termin entitet)
            {
                try
                {
                    komanda = new MySqlCommand("delete from Termin where ID_Termin = " + entitet.ID_Termin + ";", con);
                    komanda.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public List<Termin> getAll()
            {
                try
                {
                    komanda = new MySqlCommand("select * from Termin", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    List<Termin> termini = new List<Termin>();
                    while (citac.Read())
                        termini.Add(new Termin(citac.GetString("ID_Termin"), citac.GetInt32("BrojLjudi"),  citac.GetString("ID_Instruktor"), citac.GetString("ID_Predmeta")));
                    return termini;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Termin> getByExample(string name, string value)
            {
                try
                {
                    komanda = new MySqlCommand("select ‘from Termin where '" + value + "'='" + name + "'", con);
                    List<Termin> termini = new List<Termin>();
                    MySqlDataReader citac = komanda.ExecuteReader();
                    while (citac.Read())
                    {
                        termini.Add(new Termin(citac.GetString("ID_Termin"), citac.GetInt32("BrojLjudi"), citac.GetString("ID_Instruktor"), citac.GetString("ID_Predmeta")));
                    }
                    return termini;
                }


                catch (Exception e)
                {
                    throw e;
                }
            }

                  public Termin getById(int id)
            {
                try
                {
                    komanda = new MySqlCommand("select ‘from Termin where '"+id + "'", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Termin termini = null;
                    while (citac.Read())
                    {
                        termini=new Termin(citac.GetString("ID_Termin"), citac.GetInt32("BrojLjudi"),citac.GetString("ID_Instruktor"), citac.GetString("ID_Predmeta"));
                    }
                    return termini;
                }
                catch (Exception e)
                {
                    throw e;
                }

            }
             

        }
    }
}
